var express = require('express');
var router = express.Router();
const xlsx = require('node-xlsx');
var sql = require('./../tool/sql');
const xlsxfile = "C:/Users/asus/Desktop/pro.xlsx"
/* GET users listing. */
router.get('/', function(req, res, next) {
  sql.find('watcher', {}, {}).then(data => {
    res.render('watch', {
      result: data
    })
  })
  
  
});

router.get('/importData', (req, res, next) => {
  let obj = xlsx.parse(xlsxfile);
  const arr = obj[0].data;
  let result = [];
  arr.map((item, index) => {
    if(index > 0) {
        var obj = {
            proid: item[0],
            proname: item[1],
            proimg: item[2],
            proprice: item[3],
            pronum: item[4],
            prodesc: item[5]
        }
        obj.date = new Date()
        result.push(obj)
    }
    
  })
  sql.remove('watcher', {}).then(() => {
    sql.insert('watcher', result).then(() => {
      res.redirect('/watch')
    })
  })
})

module.exports = router;
